﻿Imports System.Data.OleDb
Public Class Register1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("Phones").ConnectionString)
            Dim SqlString As String = "SELECT * FROM PROFILE WHERE UserName=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim dbreader As OleDbDataReader = cmd.ExecuteReader
            If dbreader.HasRows Then
                dbreader.Read()
                If Not IsDBNull(dbreader("SurName")) Then tb_Surname.Text = dbreader("Surname")
                If Not IsDBNull(dbreader("OtherName")) Then tb_Othername.Text = dbreader("OtherName")
                If Not IsDBNull(dbreader("Gender")) Then dd_Gender.SelectedValue = dbreader("Gender")
                If Not IsDBNull(dbreader("Status")) Then dd_Status.SelectedValue = dbreader("Status")
                If Not IsDBNull(dbreader("Birthday")) Then tb_Birthday.Text = dbreader("Birthday")
                If Not IsDBNull(dbreader("PreviousPhone")) Then tb_previousphone.Text = dbreader("PreviousPhone")
                If Not IsDBNull(dbreader("Country")) Then tb_Country.Text = dbreader("Country")
                If Not IsDBNull(dbreader("Location")) Then tb_Location.Text = dbreader("Location")
                If Not IsDBNull(dbreader("Email")) Then tb_Email.Text = dbreader("Email")
                'If Not IsDBNull(dbreader("Picture")) Then fp_Piture.ID = dbreader("Picture")
                oleDbConn.Close()
                oleDbConn.Dispose()
            End If

        End If
    End Sub

    Protected Sub bt_submitProfile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bt_submitProfile.Click
        'Dim Updatestring As String
        ' Dim AnotherFileName As String = ""
        'If fp_Piture.HasFile Then
        ' Dim myIdentifierCode = Guid.NewGuid()
        ' AnotherFileName = myIdentifierCode.ToString() & ".jpg"
        ' Dim FileFolder = Request.MapPath("../ProfilePictures") & "/" & AnotherFileName
        '  fp_Piture.SaveAs(FileFolder)
        ' End If
        Dim oleDbcom As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("Phones").ConnectionString)
        oleDbcom.Open()
        Dim sqlString As String = "UPDATE PROFILE SET SurName=@f1,OtherName=@f2,Gender=@f3,Status=@f4,Birthday=@f5,PreviousPhone=@f6,Country=@f7,Location=@f8,Email=@9,Picture=@f10 WHERE UserName=@f11"
        Dim cm As OleDb.OleDbCommand = New OleDb.OleDbCommand(sqlString, oleDbcom)
        Dim newFileName As String = Guid.NewGuid().ToString() & ".jpg"
        cm.Parameters.AddWithValue("@f1", tb_Surname.Text)
        cm.Parameters.AddWithValue("@f2", tb_Othername.Text)
        cm.Parameters.AddWithValue("@f3", dd_Gender.SelectedValue)
        cm.Parameters.AddWithValue("@f4", dd_Status.SelectedValue)
        cm.Parameters.AddWithValue("@f5", tb_Birthday.Text)
        cm.Parameters.AddWithValue("@f6", tb_previousphone.Text)
        cm.Parameters.AddWithValue("@f7", tb_Country.Text)
        cm.Parameters.AddWithValue("@f8", tb_Location.Text)
        cm.Parameters.AddWithValue("@f9", tb_Email.Text)
        cm.Parameters.AddWithValue("@f10", "ProfilePictures/" & newFileName)
        cm.Parameters.AddWithValue("@f11", User.Identity.Name)
        cm.CommandType = CommandType.Text
        cm.ExecuteNonQuery()
        Dim ProfilePicturesFolder As String = Request.MapPath("ProfilePictures")
        fp_Piture.SaveAs(ProfilePicturesFolder & "/" & newFileName)
        oleDbcom.Close()
        cm.Dispose()
        oleDbcom.Dispose()
        Response.Redirect("Profile.aspx?UserName=" & User.Identity.Name)
    End Sub
End Class